class EventBus {
    private listeners: { [key: string]: Function[] } = {};

    public on(event: string, listener: Function) {
        if (!this.listeners[event]) {
            this.listeners[event] = [];
        }
        this.listeners[event].push(listener);
    }

    public off(event: string, listener: Function) {
        if (!this.listeners[event]) {
            return;
        }
        this.listeners[event] = this.listeners[event].filter(
            (l) => l !== listener,
        );
    }

    public emit(event: string, ...args: unknown[]) {
        if (!this.listeners[event]) {
            return;
        }
        this.listeners[event].forEach((listener) => listener(...args));
    }
}

export const eventBus = new EventBus(); // Singleton
